<!DOCTYPE html>
<html lang=zh>
<head>
    <!-- so meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="HandheldFriendly" content="True">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=5" />
    <meta name="description" content="JavaScript中的面向对象对象是JavaScript中一个非常重要的概念，用对象来描述事物，更有利于我们将现实的事物抽象成代码中的数据结构。 早期使用创建对象的方式最多的是使用Object类，并且使用new关键字来创建一个对象： 123var obj &#x3D; new Object()obj.name &#x3D; &amp;#x27;curry&amp;#x27;obj.age &#x3D; 33  后来很多开发者为了方便起见，都">
<meta property="og:type" content="article">
<meta property="og:title" content="面向对象">
<meta property="og:url" content="https://danyuan30.gitee.io/2021/10/11/JavaScript/oop/index.html">
<meta property="og:site_name" content="柯里的语法糖">
<meta property="og:description" content="JavaScript中的面向对象对象是JavaScript中一个非常重要的概念，用对象来描述事物，更有利于我们将现实的事物抽象成代码中的数据结构。 早期使用创建对象的方式最多的是使用Object类，并且使用new关键字来创建一个对象： 123var obj &#x3D; new Object()obj.name &#x3D; &amp;#x27;curry&amp;#x27;obj.age &#x3D; 33  后来很多开发者为了方便起见，都">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://codertzm.oss-cn-chengdu.aliyuncs.com/image-20211203195910947.png">
<meta property="og:image" content="https://codertzm.oss-cn-chengdu.aliyuncs.com/image-20211203205004143.png">
<meta property="og:image" content="https://codertzm.oss-cn-chengdu.aliyuncs.com/image-20211203210808461.png">
<meta property="og:image" content="https://codertzm.oss-cn-chengdu.aliyuncs.com/image-20211203212529228.png">
<meta property="og:image" content="https://codertzm.oss-cn-chengdu.aliyuncs.com/20190311194017886.png">
<meta property="article:published_time" content="2021-10-10T16:00:00.000Z">
<meta property="article:modified_time" content="2022-03-05T12:31:10.768Z">
<meta property="article:author" content="柯里的语法糖">
<meta property="article:tag" content="JavaScript">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://codertzm.oss-cn-chengdu.aliyuncs.com/image-20211203195910947.png">
    
    
      
        
          <link rel="shortcut icon" href="https://codertzm.oss-cn-chengdu.aliyuncs.com/favicon.ico">
        
      
      
        
          <link rel="icon" type="image/png" href="https://codertzm.oss-cn-chengdu.aliyuncs.com/favicon.ico" sizes="192x192">
        
      
      
        
          <link rel="apple-touch-icon" sizes="180x180" href="https://codertzm.oss-cn-chengdu.aliyuncs.com/favicon.ico">
        
      
    
    <!-- title -->
    <title>面向对象</title>
    <!-- styles -->
    
<link rel="stylesheet" href="/css/style.css">

    <!-- persian styles -->
    
    <!-- rss -->
    
    
	<!-- mathjax -->
	
<meta name="generator" content="Hexo 6.0.0"></head>

<body class="max-width mx-auto px3 ltr">
    
      <div id="header-post">
  <a id="menu-icon" href="#" aria-label="Menu"><i class="fas fa-bars fa-lg"></i></a>
  <a id="menu-icon-tablet" href="#" aria-label="Menu"><i class="fas fa-bars fa-lg"></i></a>
  <a id="top-icon-tablet" href="#" aria-label="Top" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');" style="display:none;"><i class="fas fa-chevron-up fa-lg"></i></a>
  <span id="menu">
    <span id="nav">
      <ul>
        <!--
       --><li><a href="/">首页</a></li><!--
     --><!--
       --><li><a href="/tags/">标签</a></li><!--
     --><!--
       --><li><a href="/categories/">分类</a></li><!--
     --><!--
       --><li><a href="/archives/">归档</a></li><!--
     --><!--
       --><li><a target="_blank" rel="noopener" href="http://github.com/twitzz">项目</a></li><!--
     --><!--
       --><li><a href="/about/">关于</a></li><!--
     --><!--
       --><li><a href="/search/">搜索</a></li><!--
     -->
      </ul>
    </span>
    <br/>
    <span id="actions">
      <ul>
        
        <li><a class="icon" aria-label="上一篇" href="/2021/11/01/JavaScript/arrayLike/"><i class="fas fa-chevron-left" aria-hidden="true" onmouseover="$('#i-prev').toggle();" onmouseout="$('#i-prev').toggle();"></i></a></li>
        
        
        <li><a class="icon" aria-label="返回顶部" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up" aria-hidden="true" onmouseover="$('#i-top').toggle();" onmouseout="$('#i-top').toggle();"></i></a></li>
        <li><a class="icon" aria-label="分享文章" href="#"><i class="fas fa-share-alt" aria-hidden="true" onmouseover="$('#i-share').toggle();" onmouseout="$('#i-share').toggle();" onclick="$('#share').toggle();return false;"></i></a></li>
      </ul>
      <span id="i-prev" class="info" style="display:none;">上一篇</span>
      <span id="i-next" class="info" style="display:none;">下一篇</span>
      <span id="i-top" class="info" style="display:none;">返回顶部</span>
      <span id="i-share" class="info" style="display:none;">分享文章</span>
    </span>
    <br/>
    <div id="share" style="display: none">
      <ul>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.facebook.com/sharer.php?u=https://danyuan30.gitee.io/2021/10/11/JavaScript/oop/"><i class="fab fa-facebook " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://twitter.com/share?url=https://danyuan30.gitee.io/2021/10/11/JavaScript/oop/&text=面向对象"><i class="fab fa-twitter " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.linkedin.com/shareArticle?url=https://danyuan30.gitee.io/2021/10/11/JavaScript/oop/&title=面向对象"><i class="fab fa-linkedin " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://pinterest.com/pin/create/bookmarklet/?url=https://danyuan30.gitee.io/2021/10/11/JavaScript/oop/&is_video=false&description=面向对象"><i class="fab fa-pinterest " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=面向对象&body=Check out this article: https://danyuan30.gitee.io/2021/10/11/JavaScript/oop/"><i class="fas fa-envelope " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://getpocket.com/save?url=https://danyuan30.gitee.io/2021/10/11/JavaScript/oop/&title=面向对象"><i class="fab fa-get-pocket " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://reddit.com/submit?url=https://danyuan30.gitee.io/2021/10/11/JavaScript/oop/&title=面向对象"><i class="fab fa-reddit " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.stumbleupon.com/submit?url=https://danyuan30.gitee.io/2021/10/11/JavaScript/oop/&title=面向对象"><i class="fab fa-stumbleupon " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://digg.com/submit?url=https://danyuan30.gitee.io/2021/10/11/JavaScript/oop/&title=面向对象"><i class="fab fa-digg " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.tumblr.com/share/link?url=https://danyuan30.gitee.io/2021/10/11/JavaScript/oop/&name=面向对象&description="><i class="fab fa-tumblr " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://news.ycombinator.com/submitlink?u=https://danyuan30.gitee.io/2021/10/11/JavaScript/oop/&t=面向对象"><i class="fab fa-hacker-news " aria-hidden="true"></i></a></li>
</ul>

    </div>
    <div id="toc">
      <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#JavaScript%E4%B8%AD%E7%9A%84%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1"><span class="toc-number">1.</span> <span class="toc-text">JavaScript中的面向对象</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AF%B9%E8%B1%A1%E4%B8%AD%E5%B1%9E%E6%80%A7"><span class="toc-number">2.</span> <span class="toc-text">对象中属性</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Object-defineProperty"><span class="toc-number">2.1.</span> <span class="toc-text">Object.defineProperty()</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%B1%9E%E6%80%A7%E6%8F%8F%E8%BF%B0%E7%AC%A6"><span class="toc-number">3.</span> <span class="toc-text">属性描述符</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E5%B1%9E%E6%80%A7%E6%8F%8F%E8%BF%B0%E7%AC%A6"><span class="toc-number">3.1.</span> <span class="toc-text">数据属性描述符</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#configurable"><span class="toc-number">3.1.1.</span> <span class="toc-text">configurable</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#enumerable"><span class="toc-number">3.1.2.</span> <span class="toc-text">enumerable</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#writable"><span class="toc-number">3.1.3.</span> <span class="toc-text">writable</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#value"><span class="toc-number">3.1.4.</span> <span class="toc-text">value</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AD%98%E5%82%A8%E5%B1%9E%E6%80%A7%E6%8F%8F%E8%BF%B0%E7%AC%A6"><span class="toc-number">3.2.</span> <span class="toc-text">存储属性描述符</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#get"><span class="toc-number">3.2.1.</span> <span class="toc-text">get</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#set"><span class="toc-number">3.2.2.</span> <span class="toc-text">set</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%90%8C%E6%97%B6%E5%AE%9A%E4%B9%89%E5%A4%9A%E4%B8%AA%E5%B1%9E%E6%80%A7"><span class="toc-number">3.3.</span> <span class="toc-text">同时定义多个属性</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E5%AF%B9%E8%B1%A1%E7%9A%84%E5%87%A0%E7%A7%8D%E6%96%B9%E6%A1%88"><span class="toc-number">4.</span> <span class="toc-text">创建对象的几种方案</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F"><span class="toc-number">4.1.</span> <span class="toc-text">工厂模式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0"><span class="toc-number">4.2.</span> <span class="toc-text">构造函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#new-%E5%85%B3%E9%94%AE%E5%AD%97"><span class="toc-number">4.3.</span> <span class="toc-text">new 关键字</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%AE%A4%E8%AF%86%E5%AF%B9%E8%B1%A1%E7%9A%84%E5%8E%9F%E5%9E%8B"><span class="toc-number">5.</span> <span class="toc-text">认识对象的原型</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8E%9F%E5%9E%8B%E5%AF%B9%E8%B1%A1"><span class="toc-number">5.1.</span> <span class="toc-text">原型对象</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%90%86%E8%A7%A3%E5%88%9B%E5%BB%BA%E5%AF%B9%E8%B1%A1%E6%97%B6%E7%9A%84%E5%86%85%E5%AD%98%E8%A1%A8%E7%8E%B0"><span class="toc-number">5.2.</span> <span class="toc-text">理解创建对象时的内存表现</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#constructor%E5%B1%9E%E6%80%A7"><span class="toc-number">5.3.</span> <span class="toc-text">constructor属性</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0%E5%92%8C%E5%8E%9F%E5%9E%8B%E7%BB%84%E5%90%88%E5%88%9B%E5%BB%BA%E5%AF%B9%E8%B1%A1"><span class="toc-number">5.4.</span> <span class="toc-text">构造函数和原型组合创建对象</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BB%A7%E6%89%BF"><span class="toc-number">6.</span> <span class="toc-text">继承</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8E%9F%E5%9E%8B%E9%93%BE"><span class="toc-number">6.1.</span> <span class="toc-text">原型链</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8E%9F%E5%9E%8B%E9%93%BE%E5%AE%9E%E7%8E%B0%E7%BB%A7%E6%89%BF"><span class="toc-number">6.2.</span> <span class="toc-text">原型链实现继承</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%80%9F%E7%94%A8%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0%E7%BB%A7%E6%89%BF"><span class="toc-number">6.3.</span> <span class="toc-text">借用构造函数继承</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AF%84%E7%94%9F%E5%BC%8F%E7%BB%A7%E6%89%BF"><span class="toc-number">6.4.</span> <span class="toc-text">寄生式继承</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9C%80%E7%BB%88%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88-%E5%AF%84%E7%94%9F%E7%BB%84%E5%90%88%E5%BC%8F%E7%BB%A7%E6%89%BF"><span class="toc-number">6.5.</span> <span class="toc-text">最终解决方案-寄生组合式继承</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#proto-%E3%80%81prototype%E3%80%81constructor"><span class="toc-number">7.</span> <span class="toc-text">__proto__、prototype、constructor</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#proto"><span class="toc-number">7.1.</span> <span class="toc-text">proto</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#prototype"><span class="toc-number">7.2.</span> <span class="toc-text">prototype</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#constructor-NaN"><span class="toc-number">7.3.</span> <span class="toc-text">constructor</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%80%BB%E7%BB%93"><span class="toc-number">8.</span> <span class="toc-text">总结</span></a></li></ol>
    </div>
  </span>
</div>

    
    <div class="content index py4">
        
        <article class="post" itemscope itemtype="http://schema.org/BlogPosting">
  <header>
    
    <h1 class="posttitle" itemprop="name headline">
        面向对象
    </h1>



    <div class="meta">
      <span class="author" itemprop="author" itemscope itemtype="http://schema.org/Person">
        <span itemprop="name">柯里的语法糖</span>
      </span>
      
    <div class="postdate">
      
        <time datetime="2021-10-10T16:00:00.000Z" itemprop="datePublished">2021-10-11</time>
        
      
    </div>


      
    <div class="article-category">
        <i class="fas fa-archive"></i>
        <a class="category-link" href="/categories/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80/">前端基础</a>
    </div>


      
    <div class="article-tag">
        <i class="fas fa-tag"></i>
        <a class="tag-link-link" href="/tags/JavaScript/" rel="tag">JavaScript</a>
    </div>


    </div>
  </header>
  

  <div class="content" itemprop="articleBody">
    <h2 id="JavaScript中的面向对象"><a href="#JavaScript中的面向对象" class="headerlink" title="JavaScript中的面向对象"></a>JavaScript中的面向对象</h2><p>对象是JavaScript中一个非常重要的概念，用对象来描述事物，更有利于我们将现实的事物抽象成代码中的数据结构。</p>
<p>早期使用创建对象的方式最多的是使用Object类，并且使用new关键字来创建一个对象：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> obj = <span class="keyword">new</span> <span class="built_in">Object</span>()</span><br><span class="line">obj.name = <span class="string">&#x27;curry&#x27;</span></span><br><span class="line">obj.age = <span class="number">33</span></span><br></pre></td></tr></table></figure>

<p>后来很多开发者为了方便起见，都是直接通过字面量的形式来创建对象：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> obj = &#123;</span><br><span class="line">  <span class="attr">name</span>:<span class="string">&#x27;curry&#x27;</span>,</span><br><span class="line">  <span class="attr">age</span>:<span class="number">33</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="对象中属性"><a href="#对象中属性" class="headerlink" title="对象中属性"></a>对象中属性</h2><p>在前面的两个例子中，我们使用了两种方式创建了对象，并且将属性直接定义或添加在对象内部，但其实我们还有另外的方式添加属性，并且为属性添加一些特性，这就是<code>Object.defineProperty()</code>。这个方法可以对属性进行添加和修改。</p>
<h3 id="Object-defineProperty"><a href="#Object-defineProperty" class="headerlink" title="Object.defineProperty()"></a>Object.defineProperty()</h3><p><code>Object.defineProperty(obj,prop,descriptor)</code>方法会直接在一个对象上定义一个新属性，或者修改一个对象的现有属性，并返回此对象。</p>
<p>它可以接受三个参数：</p>
<ul>
<li>obj：要定义属性的对象</li>
<li>prop：要定义或修改的对象的属性名称或Symbol</li>
<li>descriptor：要定义或修改的描述符</li>
</ul>
<p>它的返回值是：</p>
<ul>
<li>被传递给函数的对象</li>
</ul>
<h2 id="属性描述符"><a href="#属性描述符" class="headerlink" title="属性描述符"></a>属性描述符</h2><p>属性描述符分为两种：</p>
<ol>
<li>数据属性描述符</li>
<li>存取属性描述符</li>
</ol>
<table>
<thead>
<tr>
<th></th>
<th>configurable</th>
<th>enumerable</th>
<th>value</th>
<th>writable</th>
<th>get</th>
<th>set</th>
</tr>
</thead>
<tbody><tr>
<td>数据属性描述符</td>
<td>可以</td>
<td>可以</td>
<td>可以</td>
<td>可以</td>
<td>不可以</td>
<td>不可以</td>
</tr>
<tr>
<td>存取属性描述符</td>
<td>可以</td>
<td>可以</td>
<td>不可以</td>
<td>不可以</td>
<td>可以</td>
<td>可以</td>
</tr>
</tbody></table>
<p>上面这个表格表示：<code>get</code>与<code>set</code>不能与<code>value</code>和<code>writable</code><strong>同时出现</strong>。</p>
<h3 id="数据属性描述符"><a href="#数据属性描述符" class="headerlink" title="数据属性描述符"></a>数据属性描述符</h3><p>该描述符有如下四个特性：</p>
<h4 id="configurable"><a href="#configurable" class="headerlink" title="configurable"></a>configurable</h4><p>表示属性是否可以通过<code>delete</code>删除属性，是否可以修改它的特性，或者是否可以将它修改为存取属性描述符。</p>
<ul>
<li><p>当我们直接在一个对象上定义某个属性时，这个属性的<code>configurable</code>为<code>true</code>。</p>
</li>
<li><p>当我们通过属性描述符定义一个属性时，这个属性的<code>configurable</code>默认为<code>false</code>。</p>
</li>
</ul>
<p>例子：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> obj = &#123;</span><br><span class="line">  <span class="attr">name</span>:<span class="string">&#x27;curry&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">Object</span>.defineProperty(obj,<span class="string">&#x27;name&#x27;</span>,&#123;</span><br><span class="line">  <span class="attr">configurable</span>:<span class="literal">false</span> <span class="comment">// 不可配置</span></span><br><span class="line">&#125;)</span><br><span class="line"><span class="keyword">delete</span> obj.name</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(obj) <span class="comment">// &#123; name: &#x27;curry&#x27; &#125;</span></span><br></pre></td></tr></table></figure>

<h4 id="enumerable"><a href="#enumerable" class="headerlink" title="enumerable"></a>enumerable</h4><p>表示属性是否可以通过<code>for-in</code>或者<code>Object.keys()</code>返回该属性。</p>
<ul>
<li><p>当我们直接在一个对象上定义某个属性时，这个属性的<code>enumerable</code>为<code>true</code>。</p>
</li>
<li><p>当我们通过属性描述符定义一个属性时，这个属性的<code>enumerable</code>默认为<code>false</code>。</p>
</li>
</ul>
<p>例子：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> obj = &#123;</span><br><span class="line">  <span class="attr">name</span>:<span class="string">&#x27;curry&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Object</span>.keys(obj)) [<span class="string">&#x27;name&#x27;</span>]</span><br><span class="line"></span><br><span class="line"><span class="built_in">Object</span>.defineProperty(obj,<span class="string">&#x27;name&#x27;</span>,&#123;</span><br><span class="line">  <span class="attr">enumerable</span>:<span class="literal">false</span> <span class="comment">// 不可枚举</span></span><br><span class="line">&#125;)</span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Object</span>.keys(obj)) <span class="comment">// []</span></span><br></pre></td></tr></table></figure>

<h4 id="writable"><a href="#writable" class="headerlink" title="writable"></a>writable</h4><p>表示是否可以修改属性的值。</p>
<ul>
<li><p>当我们直接在一个对象上定义某个属性时，这个属性的<code>writable</code>为<code>true</code>。</p>
</li>
<li><p>当我们通过属性描述符定义一个属性时，这个属性的<code>writable</code>默认为<code>false</code>。</p>
</li>
</ul>
<p>例子：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> obj = &#123;</span><br><span class="line">  <span class="attr">name</span>:<span class="string">&#x27;curry&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">Object</span>.defineProperty(obj,<span class="string">&#x27;name&#x27;</span>,&#123;</span><br><span class="line">  <span class="attr">writable</span>:<span class="literal">false</span> <span class="comment">// 不可修改</span></span><br><span class="line">&#125;)</span><br><span class="line">obj.name = <span class="string">&#x27;james&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(obj) <span class="comment">// &#123; name: &#x27;curry&#x27; &#125;</span></span><br></pre></td></tr></table></figure>

<h4 id="value"><a href="#value" class="headerlink" title="value"></a>value</h4><p>表示属性的值，读取属性时会返回该值，修改属性时，会对其进行修改。</p>
<ul>
<li>默认情况下这个值是<code>undefined</code>。</li>
</ul>
<p>例子：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> obj = &#123;</span><br><span class="line">  <span class="attr">name</span>: <span class="string">&#x27;curry&#x27;</span>,</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">Object</span>.defineProperty(obj, <span class="string">&#x27;name&#x27;</span>, &#123;</span><br><span class="line">  <span class="attr">value</span>: <span class="string">&#x27;james&#x27;</span>, <span class="comment">// 属性的值</span></span><br><span class="line">&#125;)</span><br><span class="line"><span class="built_in">console</span>.log(obj) <span class="comment">// &#123; name: &#x27;james&#x27; &#125;</span></span><br></pre></td></tr></table></figure>

<h3 id="存储属性描述符"><a href="#存储属性描述符" class="headerlink" title="存储属性描述符"></a>存储属性描述符</h3><p>存储属性描述符中的<code>configurable</code>和<code>writable</code>与数据属性描述符相同，这里不再说明。</p>
<h4 id="get"><a href="#get" class="headerlink" title="get"></a>get</h4><p>获取属性时会执行的函数。默认为<code>undefined</code></p>
<p>例子：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> obj = &#123;</span><br><span class="line">  <span class="attr">name</span>: <span class="string">&#x27;curry&#x27;</span>,</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">Object</span>.defineProperty(obj, <span class="string">&#x27;name&#x27;</span>, &#123;</span><br><span class="line">  <span class="function"><span class="title">get</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&#x27;abc&#x27;</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(obj.name) <span class="comment">// abc</span></span><br></pre></td></tr></table></figure>

<h4 id="set"><a href="#set" class="headerlink" title="set"></a>set</h4><p>修改属性时会执行的函数。默认为<code>undefined</code></p>
<p>例子：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> obj = &#123;</span><br><span class="line">  <span class="attr">name</span>: <span class="string">&#x27;curry&#x27;</span>,</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> age = <span class="number">32</span></span><br><span class="line"><span class="built_in">Object</span>.defineProperty(obj, <span class="string">&#x27;age&#x27;</span>, &#123;</span><br><span class="line">  <span class="function"><span class="title">get</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> age</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="function"><span class="title">set</span>(<span class="params">newValue</span>)</span>&#123;</span><br><span class="line">    age = newValue</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br><span class="line">obj.age = <span class="number">33</span></span><br><span class="line"><span class="built_in">console</span>.log(obj.age)</span><br></pre></td></tr></table></figure>

<h3 id="同时定义多个属性"><a href="#同时定义多个属性" class="headerlink" title="同时定义多个属性"></a>同时定义多个属性</h3><p><code>Object.defineProperties()</code>方法直接在一个对象上定义 <strong>多个</strong> 新的属性或修改现有属性，并且返回该对象。</p>
<p>例子：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> obj = &#123;</span><br><span class="line">  <span class="attr">name</span>: <span class="string">&#x27;curry&#x27;</span>,</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">Object</span>.defineProperties(obj, &#123;</span><br><span class="line">  <span class="attr">name</span>:&#123;</span><br><span class="line">    <span class="attr">value</span>:<span class="string">&#x27;james&#x27;</span></span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">age</span>:&#123;</span><br><span class="line">    <span class="attr">enumerable</span>:<span class="literal">false</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Object</span>.keys(obj)) <span class="comment">// [&#x27;name&#x27;]</span></span><br></pre></td></tr></table></figure>

<h2 id="创建对象的几种方案"><a href="#创建对象的几种方案" class="headerlink" title="创建对象的几种方案"></a>创建对象的几种方案</h2><p>在本文开始我们提到两种创建对象的方式，但其实还有很多种创建对象的方式，比如：<strong>工厂模式</strong>。我们来看看工厂模式创建对象的方法。</p>
<h3 id="工厂模式"><a href="#工厂模式" class="headerlink" title="工厂模式"></a>工厂模式</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">createPerson</span>(<span class="params">name,age</span>)</span>&#123;</span><br><span class="line">  <span class="keyword">var</span> p = <span class="keyword">new</span> <span class="built_in">Object</span>()</span><br><span class="line">  p.name = name</span><br><span class="line">  p.age = age</span><br><span class="line">  <span class="keyword">return</span> p</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> p1 = createPerson(<span class="string">&#x27;curry&#x27;</span>,<span class="number">33</span>)</span><br><span class="line"><span class="keyword">var</span> p2 = createPerson(<span class="string">&#x27;james&#x27;</span>,<span class="number">37</span>)</span><br><span class="line"><span class="built_in">console</span>.log(p1,p2) <span class="comment">// &#123; name: &#x27;curry&#x27;, age :33 &#125; &#123; name: &quot;james&quot;, age:37 &#125;</span></span><br></pre></td></tr></table></figure>

<p>通过工厂模式创建对象有一个很大的<strong>弊端</strong>：它们创建的对象都是<code>Object</code>类型的。</p>
<p>因此接下来我们来说另外一种方式：构造函数的方式。</p>
<h3 id="构造函数"><a href="#构造函数" class="headerlink" title="构造函数"></a>构造函数</h3><p>来看看构造函数的方式是如何创建对象的。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Player</span>(<span class="params">name,age</span>)</span>&#123;</span><br><span class="line">  <span class="built_in">this</span>.name = name</span><br><span class="line">  <span class="built_in">this</span>.age = age</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> p1 = <span class="keyword">new</span> Player(<span class="string">&#x27;curry&#x27;</span>,<span class="number">33</span>)</span><br></pre></td></tr></table></figure>

<p>与其他编程语言有些不同，JavaScript中的构造函数和其他普普通通的函数<strong>相同</strong>。</p>
<p>但是，一个函数<strong>被<code>new</code>关键字调用</strong>，它就是一个构造函数。</p>
<p><strong><code>new</code>关键字调用到底发生了什么？</strong>为什么使用它调用后，一个普通的函数就变为了构造函数？这不禁是我们想要问的问题。</p>
<h3 id="new-关键字"><a href="#new-关键字" class="headerlink" title="new 关键字"></a>new 关键字</h3><p>如果一个函数被new关键字调用了，它就会执行如下操作：</p>
<ol>
<li>在内存中<strong>创建一个新对象</strong></li>
<li>这个新对象的内部 [[Prototype]] 特性被赋值为<strong>构造函数的prototype属性</strong>：</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">p1.__proto__ = Player.prototype</span><br></pre></td></tr></table></figure>

<ol start="3">
<li><strong>构造函数内部的this</strong>被赋值给新的对象 也就是新对象和函数调用的this会绑定起来</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Player.call(p1,<span class="string">&#x27;curry&#x27;</span>);</span><br></pre></td></tr></table></figure>

<ol start="4">
<li>执行构造函数内部的代码 即给新对象添加属性和方法</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">p1.name;</span><br><span class="line"><span class="built_in">console</span>.log(p1);</span><br></pre></td></tr></table></figure>

<ol start="5">
<li>如果构造函数返回非空对象，则返回该对象；否则，<strong>返回刚创建的新对象</strong></li>
</ol>
<p>这样，这个构造函数可以确保我们创建的对象是属于使用构造函数创建的那个类型的对象，而不再仅仅像工厂模式一样都是<code>Object</code>类型的对象。</p>
<h2 id="认识对象的原型"><a href="#认识对象的原型" class="headerlink" title="认识对象的原型"></a>认识对象的原型</h2><p>在JavaScript中，每个对象都有一个内置的特殊属性<code>[[prototype]]</code>，这个特殊的属性指向另外一个对象。</p>
<h3 id="原型对象"><a href="#原型对象" class="headerlink" title="原型对象"></a>原型对象</h3><p>这个对象就是我们常说的<strong>原型对象</strong>。那么它有什么用呢？</p>
<ul>
<li>当我们通过引用对象的属性key来获取一个value时，它会触发 [[Get]]的操作。</li>
<li>这个操作会首先检查该属性是否有对应的属性，如果有的话就使用它。</li>
<li>如果对象中没有改属性，那么会访问对象的[[prototype]]属性指向的那个对象上的属性。</li>
</ul>
<p>了解了它的作用后，我们又想问：如何获取到这个对象？</p>
<p>获取的方式有两种：</p>
<ol>
<li>通过对象的<code>__proto__</code>属性可以获取到，（早期浏览器添加的，存在一定兼容性问题）。</li>
<li>通过<code>Object.getPrototypeOf()</code>方法可以获取到。</li>
</ol>
<blockquote>
<p>注意：prototype属性时函数独有的</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> obj = &#123;&#125;</span><br><span class="line"><span class="built_in">console</span>.log(obj.prototype) <span class="comment">// undefined</span></span><br></pre></td></tr></table></figure>

<h3 id="理解创建对象时的内存表现"><a href="#理解创建对象时的内存表现" class="headerlink" title="理解创建对象时的内存表现"></a>理解创建对象时的内存表现</h3><p>我们前面在说<code>new</code>关键字的时候说到过，其中一个步骤就是跟原型相关</p>
<blockquote>
<p>这个新对象的内部<code>[[Prototype]]</code> 特性被赋值为<strong>构造函数的<code>prototype</code>属性</strong>：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">p1.__proto__ = Player.prototype</span><br></pre></td></tr></table></figure>

</blockquote>
<p>它在内存中的表现大概是这样的（我们稍后来说<code>constructor</code>）：</p>
<p><img src="https://codertzm.oss-cn-chengdu.aliyuncs.com/image-20211203195910947.png" alt="image-20211203195910947"></p>
<p>这意味着我们通过<code>Player</code>构造函数创建出来的所有对象的<code>[[prototype]]</code>属性都指向<code>Plyaer.prototype</code>。</p>
<h3 id="constructor属性"><a href="#constructor属性" class="headerlink" title="constructor属性"></a>constructor属性</h3><p>默认情况下原型上都会添加一个属性叫做<code>constructor</code>，这个<code>constructor</code>指回构造函数。</p>
<p>当我们需要在原型对象上新增多个属性时，一般会重写原型对象：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Player</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">Player.prototype = &#123;</span><br><span class="line">  <span class="attr">name</span>:<span class="string">&#x27;curry&#x27;</span>,</span><br><span class="line">  <span class="function"><span class="title">eating</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">  &#125;,</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>此时，<code>Player.prototype</code>指向了一个普通对象，它上面将不会再有<code>constructor</code>属性了。</p>
<p>我们也可以手动将<code>constructor</code><strong>指回构造函数</strong>：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Player.prototype = &#123;</span><br><span class="line">  <span class="attr">constructor</span>:Player</span><br><span class="line">  ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这种方式虽然可行，但是会造成<code>constructor</code>变为可枚举属性。（默认情况下，<code>constructor</code>属性不可枚举）。</p>
<p>这个时候，就可以使用我们之前提到过的<code>Object.defineProperty()</code></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Object</span>.defineProperty(Player,prototype,<span class="string">&#x27;constructor&#x27;</span>,&#123;</span><br><span class="line">  <span class="attr">enumerable</span>:<span class="literal">false</span>,</span><br><span class="line">  <span class="attr">value</span>:Player</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<h3 id="构造函数和原型组合创建对象"><a href="#构造函数和原型组合创建对象" class="headerlink" title="构造函数和原型组合创建对象"></a>构造函数和原型组合创建对象</h3><p>有了原型和构造函数后，我们可以将一些重复出现的方法放在原型对象上供所有实例对象上使用。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Player</span> (<span class="params">name</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">this</span>.name = name</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">Player.prototype.running = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="built_in">this</span>.name + <span class="string">&#x27; running&#x27;</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">Player.prototype.eating = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="built_in">this</span>.name + <span class="string">&#x27; eating&#x27;</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> p1 = <span class="keyword">new</span> Player(<span class="string">&#x27;curry&#x27;</span>)</span><br><span class="line"><span class="keyword">var</span> p2 = <span class="keyword">new</span> Player(<span class="string">&#x27;james&#x27;</span>)</span><br><span class="line">p1.running() <span class="comment">// curry running</span></span><br><span class="line">p2.eating() <span class="comment">// james eating</span></span><br></pre></td></tr></table></figure>

<h2 id="继承"><a href="#继承" class="headerlink" title="继承"></a>继承</h2><p>面向对象有三大特性：封装、继承、多态。我们着重聊聊继承。</p>
<p><strong>继承的作用：可以将重复的代码逻辑抽取到父类中使用，子类可以通过继承父类来使用这些代码</strong>。</p>
<p>在聊继承前，我们需要明白JavaScript中的<strong>原型链</strong>。</p>
<h3 id="原型链"><a href="#原型链" class="headerlink" title="原型链"></a>原型链</h3><p>当我们获取对象的属性的时候，如果没有从当前的属性中查找到属性，就会从它的原型对象上查找，直到找到<code>Object.prototype</code>。</p>
<p>为什么会找到<code>Object.prototype</code>就不往上找了呢？这是因为<code>Object.prototype</code>中的<code>__proto__</code>指向<code>null</code>，这就代表它是<strong>顶层对象</strong>了。</p>
<p>在内存中表现具体如下：</p>
<p><img src="https://codertzm.oss-cn-chengdu.aliyuncs.com/image-20211203205004143.png" alt="image-20211203205004143"></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">p1.toString()</span><br></pre></td></tr></table></figure>

<p>如果我们需要调用<code>p1</code>的<code>toString()</code>方法，就会从当前对象<code>p1</code>查找这个方法，然后通过<code>__proto__</code>属性去它的原型对象上找，直到找到该属性，没有找到则会找到<code>Object.prototype</code>这个对象结束。</p>
<p><strong>这就是原型链！</strong></p>
<h3 id="原型链实现继承"><a href="#原型链实现继承" class="headerlink" title="原型链实现继承"></a>原型链实现继承</h3><p>了解了原型链后，我们可以来说一说JavaScript中的继承了，我们先来说第一种继承方式：<strong>通过原型链实现继承</strong>。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Person</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">this</span>.friends = []</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">Person.prototype.running = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="built_in">this</span>.name + <span class="string">&#x27; running&#x27;</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Student</span>(<span class="params">name, age</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">this</span>.name = name</span><br><span class="line">  <span class="built_in">this</span>.age = age</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> p = <span class="keyword">new</span> Person()</span><br><span class="line">Student.prototype = p</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> s1 = <span class="keyword">new</span> Student(<span class="string">&#x27;curry&#x27;</span>,<span class="number">33</span>)</span><br><span class="line"><span class="keyword">var</span> s2 = <span class="keyword">new</span> Student(<span class="string">&#x27;james&#x27;</span>,<span class="number">37</span>)</span><br><span class="line"></span><br><span class="line">s1.friends.push(<span class="string">&#x27;kobe&#x27;</span>)</span><br><span class="line">s2.friends.push(<span class="string">&#x27;green&#x27;</span>)</span><br><span class="line"></span><br><span class="line">s1.running() <span class="comment">// curry running</span></span><br><span class="line"><span class="built_in">console</span>.log(s1) <span class="comment">// &#123; name: &#x27;curry&#x27;, age: 33 &#125;</span></span><br><span class="line"><span class="built_in">console</span>.log(s1.friends) <span class="comment">// [ &#x27;kobe&#x27;, &#x27;green&#x27; ]</span></span><br><span class="line"><span class="built_in">console</span>.log(s2.friends) <span class="comment">// [ &#x27;kobe&#x27;, &#x27;green&#x27; ]</span></span><br></pre></td></tr></table></figure>

<p>它的内存表现如下：</p>
<p><img src="https://codertzm.oss-cn-chengdu.aliyuncs.com/image-20211203210808461.png" alt="image-20211203210808461"></p>
<p>我们从上面这个例子可以看出原型链继承存在几个弊端：</p>
<ol>
<li>我们通过直接打印对象是看不到原型对象的属性。</li>
<li>属性会被多个对象共享，如果这个对象是一个引用类型，那么就会造成多个属性操作同一个引用。</li>
</ol>
<h3 id="借用构造函数继承"><a href="#借用构造函数继承" class="headerlink" title="借用构造函数继承"></a>借用构造函数继承</h3><p>为了解决原型链继承中存在的问题，开发人员提供了一种新的技术: <strong>借用构造函数（constructor stealing）</strong>，也称之为组合继承。</p>
<p>这种继承方式很简单：在子类型构造函数中的内部<strong>调用了父类的构造函数</strong>。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Person</span>(<span class="params">friends</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">this</span>.friends = friends</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">Person.prototype.running = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="built_in">this</span>.name + <span class="string">&#x27; running&#x27;</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Student</span>(<span class="params">name, age,friends</span>) </span>&#123;</span><br><span class="line">  Person.call(<span class="built_in">this</span>,friends)</span><br><span class="line">  <span class="built_in">this</span>.name = name</span><br><span class="line">  <span class="built_in">this</span>.age = age</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 该对象上会多出来friends属性，并且它的值是undefined</span></span><br><span class="line"><span class="keyword">var</span> p = <span class="keyword">new</span> Person()</span><br><span class="line">Student.prototype = p</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> s1 = <span class="keyword">new</span> Student(<span class="string">&#x27;curry&#x27;</span>,<span class="number">33</span>,[<span class="string">&#x27;ad&#x27;</span>,<span class="string">&#x27;wade&#x27;</span>])</span><br><span class="line"><span class="keyword">var</span> s2 = <span class="keyword">new</span> Student(<span class="string">&#x27;james&#x27;</span>,<span class="number">37</span>,[<span class="string">&#x27;bosh&#x27;</span>,<span class="string">&#x27;howard&#x27;</span>])</span><br><span class="line">s1.friends.push(<span class="string">&#x27;kobe&#x27;</span>)</span><br><span class="line">s2.friends.push(<span class="string">&#x27;green&#x27;</span>)</span><br><span class="line"></span><br><span class="line">s1.running() <span class="comment">// curry running</span></span><br><span class="line"><span class="built_in">console</span>.log(s1) <span class="comment">// &#123; friends: [ &#x27;ad&#x27;, &#x27;wade&#x27;, &#x27;kobe&#x27; ], name: &#x27;curry&#x27;, age: 33 &#125;</span></span><br><span class="line"><span class="built_in">console</span>.log(s1.friends) <span class="comment">// [ &#x27;ad&#x27;, &#x27;wade&#x27;, &#x27;kobe&#x27; ]</span></span><br><span class="line"><span class="built_in">console</span>.log(s2.friends) <span class="comment">// [ &#x27;bosh&#x27;, &#x27;howard&#x27;, &#x27;green&#x27; ]</span></span><br></pre></td></tr></table></figure>

<p>它的内存表现如下：</p>
<p><img src="https://codertzm.oss-cn-chengdu.aliyuncs.com/image-20211203212529228.png" alt="image-20211203212529228"></p>
<p>借用构造函数仍然存在弊端：</p>
<ol>
<li><code>Person</code>函数被调用了两次。</li>
<li><code>s1</code>的原型对象会多出来一些属性（比如<code>friends</code>属性），但是这些属性没有存在的必要。</li>
</ol>
<h3 id="寄生式继承"><a href="#寄生式继承" class="headerlink" title="寄生式继承"></a>寄生式继承</h3><p>在说寄生式继承前，我们先来看看原型式继承函数。</p>
<p>这种模式要从道格拉斯·克罗克福德（Douglas Crockford，著名的前端大师，JSON的创立者）在2006年写的一篇文章说起: Prototypal Inheritance in JavaScript(在JS中使用原型式继承)。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 原型式继承函数</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">createObject</span>(<span class="params">o</span>)</span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">function</span> <span class="title">F</span>(<span class="params"></span>)</span>&#123;&#125;</span><br><span class="line">  <span class="comment">// 将它的原型对象指向改变</span></span><br><span class="line">  F.prototype = o</span><br><span class="line">  <span class="keyword">var</span> newObj = <span class="keyword">new</span> F()</span><br><span class="line">  <span class="comment">// 返回的对象(newObj)可以通过__proto__属性访问到对象o</span></span><br><span class="line">  <span class="keyword">return</span> newObj</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>ES6提供了<code>Object.create()</code>方法来实现原型式继承函数的作用</p>
</blockquote>
<p>寄生式继承就是将原型式继承函数和工厂模式结合起来的一种继承方式。</p>
<p>它的原理是：创建一个封装继承过程的函数, 该函数在内部以某种方式来增强对象，最后再将这个对象返回。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">createStudent</span>(<span class="params">person</span>)</span>&#123;</span><br><span class="line">  <span class="keyword">var</span> stu = <span class="built_in">Object</span>.create(person)</span><br><span class="line">  stu.studying = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;studying&quot;</span>)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>因为它使用了工厂模式，所以仍然是存在弊端工厂模式的弊端的。</p>
<h3 id="最终解决方案-寄生组合式继承"><a href="#最终解决方案-寄生组合式继承" class="headerlink" title="最终解决方案-寄生组合式继承"></a>最终解决方案-寄生组合式继承</h3><p>其实从名字就可以看出，这是将寄生式继承和组合式继承（借用构造函数）结合起来的一种继承方式。</p>
<p>我们提到过：组合式继承（借用构造函数）存在两个问题：1. 父类构造函数被调用两次；2. 实例化的子类的原型对象是会多出来一些无用的属性。</p>
<p>然而通过这种最终解决方案，这些问题都能被解决。直接看代码：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 继承的工具函数 subType-子类，SuperType-父类</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">inheritPrototype</span>(<span class="params">SubType, SuperType</span>) </span>&#123;</span><br><span class="line">  SubType.prototype = <span class="built_in">Object</span>.create(SuperType.prototype)</span><br><span class="line">  <span class="comment">// contructor指回原来的构造函数</span></span><br><span class="line">  <span class="built_in">Object</span>.defineProperty(SubType.prototype, <span class="string">&quot;constructor&quot;</span>, &#123;</span><br><span class="line">    <span class="attr">enumerable</span>: <span class="literal">false</span>,</span><br><span class="line">    <span class="attr">configurable</span>: <span class="literal">true</span>,</span><br><span class="line">    <span class="attr">writable</span>: <span class="literal">true</span>,</span><br><span class="line">    <span class="attr">value</span>: SubType</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 父类Person</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Person</span>(<span class="params">name, age, friends</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">this</span>.name = name</span><br><span class="line">  <span class="built_in">this</span>.age = age</span><br><span class="line">  <span class="built_in">this</span>.friends = friends</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 给原型上添加方法</span></span><br><span class="line">Person.prototype.running = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;running~&quot;</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 给原型上添加方法</span></span><br><span class="line">Person.prototype.eating = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;eating~&quot;</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 子类Student</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Student</span>(<span class="params">name, age, friends, sno, score</span>) </span>&#123;</span><br><span class="line">  <span class="comment">// 借用父类的构造函数，但是只调用了一次父类的构造函数，解决了组合继承的问题</span></span><br><span class="line">  Person.call(<span class="built_in">this</span>, name, age, friends)</span><br><span class="line">  <span class="built_in">this</span>.sno = sno</span><br><span class="line">  <span class="built_in">this</span>.score = score</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 给原型上添加方法</span></span><br><span class="line">Student.prototype.studying = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;studying~&quot;</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// Student继承Person</span></span><br><span class="line">inheritPrototype(Student, Person)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 实例化子类</span></span><br><span class="line"><span class="keyword">var</span> stu = <span class="keyword">new</span> Student(<span class="string">&quot;why&quot;</span>, <span class="number">18</span>, [<span class="string">&quot;kobe&quot;</span>], <span class="number">111</span>, <span class="number">100</span>)</span><br><span class="line"></span><br><span class="line">stu.studying() <span class="comment">// studying~</span></span><br><span class="line">stu.running() <span class="comment">// running~</span></span><br><span class="line">stu.eating() <span class="comment">// eating~</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(stu.constructor.name) <span class="comment">// Student</span></span><br></pre></td></tr></table></figure>

<h2 id="proto-、prototype、constructor"><a href="#proto-、prototype、constructor" class="headerlink" title="__proto__、prototype、constructor"></a>__proto__、prototype、constructor</h2><p>说完继承后，我们最后来完全弄懂这三个属性。</p>
<p><img src="https://codertzm.oss-cn-chengdu.aliyuncs.com/20190311194017886.png"></p>
<p>直接看这张图很容易晕，我们一步步来分解。</p>
<h3 id="proto"><a href="#proto" class="headerlink" title="proto"></a><strong>proto</strong></h3><p>我们来看看<code>__ proto __</code>属性，先只看上图红色的那条线，并且需要牢记两点：</p>
<ol>
<li><code>__proto__</code>和<code>constructor</code>属性是<strong>对象</strong>所独有的。</li>
<li><code>prototype</code>属性是<strong>函数</strong>所独有的。但是由于JS中函数也是一种对象，所以函数也拥有<code>__proto__</code>和<code>constructor</code>属性。</li>
</ol>
<h3 id="prototype"><a href="#prototype" class="headerlink" title="prototype"></a>prototype</h3><p>接着我们来看看<code>prototype</code>属性（绿色的那条线），它是<strong>函数所独有的</strong>，它是从<strong>一个函数指向一个对象</strong>。它的含义是<strong>函数的原型对象</strong>，也就是这个函数（其实所有函数都可以作为构造函数）所创建的实例的原型对象。<strong>任何函数在创建的时候，其实会默认同时创建该函数的prototype对象。</strong></p>
<h3 id="constructor-NaN"><a href="#constructor-NaN" class="headerlink" title="constructor"></a>constructor</h3><p>最后我们再来看<code>constructor</code>属性，它也是<strong>对象才拥有的</strong>，它是从<strong>一个对象指向一个函数</strong>，含义就是<strong>指向该对象的构造函数</strong>。</p>
<p>可以看出<strong>Function</strong>这个对象比较特殊，<strong>它的构造函数就是它自己</strong>（因为Function可以看成是一个函数，函数也是一个对象），所有函数和对象最终都是由Function构造函数得来，所以<code>constructor</code>属性的终点就是<strong>Function</strong>这个函数。</p>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>本文涉及的知识点较多，关于ES6中的<code>class</code>关键字我们这里不提及是因为它不过是一种语法糖而已。</p>
<p>同时只有真正掌握了JavaScript中的原型和面向对象的概念才能真正理解，JavaScript中的继承是如何来实现的。</p>

  </div>
</article>



        
          <div id="footer-post-container">
  <div id="footer-post">

    <div id="nav-footer" style="display: none">
      <ul>
         
          <li><a href="/">首页</a></li>
         
          <li><a href="/tags/">标签</a></li>
         
          <li><a href="/categories/">分类</a></li>
         
          <li><a href="/archives/">归档</a></li>
         
          <li><a target="_blank" rel="noopener" href="http://github.com/twitzz">项目</a></li>
         
          <li><a href="/about/">关于</a></li>
         
          <li><a href="/search/">搜索</a></li>
        
      </ul>
    </div>

    <div id="toc-footer" style="display: none">
      <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#JavaScript%E4%B8%AD%E7%9A%84%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1"><span class="toc-number">1.</span> <span class="toc-text">JavaScript中的面向对象</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AF%B9%E8%B1%A1%E4%B8%AD%E5%B1%9E%E6%80%A7"><span class="toc-number">2.</span> <span class="toc-text">对象中属性</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Object-defineProperty"><span class="toc-number">2.1.</span> <span class="toc-text">Object.defineProperty()</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%B1%9E%E6%80%A7%E6%8F%8F%E8%BF%B0%E7%AC%A6"><span class="toc-number">3.</span> <span class="toc-text">属性描述符</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E5%B1%9E%E6%80%A7%E6%8F%8F%E8%BF%B0%E7%AC%A6"><span class="toc-number">3.1.</span> <span class="toc-text">数据属性描述符</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#configurable"><span class="toc-number">3.1.1.</span> <span class="toc-text">configurable</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#enumerable"><span class="toc-number">3.1.2.</span> <span class="toc-text">enumerable</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#writable"><span class="toc-number">3.1.3.</span> <span class="toc-text">writable</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#value"><span class="toc-number">3.1.4.</span> <span class="toc-text">value</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AD%98%E5%82%A8%E5%B1%9E%E6%80%A7%E6%8F%8F%E8%BF%B0%E7%AC%A6"><span class="toc-number">3.2.</span> <span class="toc-text">存储属性描述符</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#get"><span class="toc-number">3.2.1.</span> <span class="toc-text">get</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#set"><span class="toc-number">3.2.2.</span> <span class="toc-text">set</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%90%8C%E6%97%B6%E5%AE%9A%E4%B9%89%E5%A4%9A%E4%B8%AA%E5%B1%9E%E6%80%A7"><span class="toc-number">3.3.</span> <span class="toc-text">同时定义多个属性</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E5%AF%B9%E8%B1%A1%E7%9A%84%E5%87%A0%E7%A7%8D%E6%96%B9%E6%A1%88"><span class="toc-number">4.</span> <span class="toc-text">创建对象的几种方案</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F"><span class="toc-number">4.1.</span> <span class="toc-text">工厂模式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0"><span class="toc-number">4.2.</span> <span class="toc-text">构造函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#new-%E5%85%B3%E9%94%AE%E5%AD%97"><span class="toc-number">4.3.</span> <span class="toc-text">new 关键字</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%AE%A4%E8%AF%86%E5%AF%B9%E8%B1%A1%E7%9A%84%E5%8E%9F%E5%9E%8B"><span class="toc-number">5.</span> <span class="toc-text">认识对象的原型</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8E%9F%E5%9E%8B%E5%AF%B9%E8%B1%A1"><span class="toc-number">5.1.</span> <span class="toc-text">原型对象</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%90%86%E8%A7%A3%E5%88%9B%E5%BB%BA%E5%AF%B9%E8%B1%A1%E6%97%B6%E7%9A%84%E5%86%85%E5%AD%98%E8%A1%A8%E7%8E%B0"><span class="toc-number">5.2.</span> <span class="toc-text">理解创建对象时的内存表现</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#constructor%E5%B1%9E%E6%80%A7"><span class="toc-number">5.3.</span> <span class="toc-text">constructor属性</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0%E5%92%8C%E5%8E%9F%E5%9E%8B%E7%BB%84%E5%90%88%E5%88%9B%E5%BB%BA%E5%AF%B9%E8%B1%A1"><span class="toc-number">5.4.</span> <span class="toc-text">构造函数和原型组合创建对象</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BB%A7%E6%89%BF"><span class="toc-number">6.</span> <span class="toc-text">继承</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8E%9F%E5%9E%8B%E9%93%BE"><span class="toc-number">6.1.</span> <span class="toc-text">原型链</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8E%9F%E5%9E%8B%E9%93%BE%E5%AE%9E%E7%8E%B0%E7%BB%A7%E6%89%BF"><span class="toc-number">6.2.</span> <span class="toc-text">原型链实现继承</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%80%9F%E7%94%A8%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0%E7%BB%A7%E6%89%BF"><span class="toc-number">6.3.</span> <span class="toc-text">借用构造函数继承</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AF%84%E7%94%9F%E5%BC%8F%E7%BB%A7%E6%89%BF"><span class="toc-number">6.4.</span> <span class="toc-text">寄生式继承</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9C%80%E7%BB%88%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88-%E5%AF%84%E7%94%9F%E7%BB%84%E5%90%88%E5%BC%8F%E7%BB%A7%E6%89%BF"><span class="toc-number">6.5.</span> <span class="toc-text">最终解决方案-寄生组合式继承</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#proto-%E3%80%81prototype%E3%80%81constructor"><span class="toc-number">7.</span> <span class="toc-text">__proto__、prototype、constructor</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#proto"><span class="toc-number">7.1.</span> <span class="toc-text">proto</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#prototype"><span class="toc-number">7.2.</span> <span class="toc-text">prototype</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#constructor-NaN"><span class="toc-number">7.3.</span> <span class="toc-text">constructor</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%80%BB%E7%BB%93"><span class="toc-number">8.</span> <span class="toc-text">总结</span></a></li></ol>
    </div>

    <div id="share-footer" style="display: none">
      <ul>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.facebook.com/sharer.php?u=https://danyuan30.gitee.io/2021/10/11/JavaScript/oop/"><i class="fab fa-facebook fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://twitter.com/share?url=https://danyuan30.gitee.io/2021/10/11/JavaScript/oop/&text=面向对象"><i class="fab fa-twitter fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.linkedin.com/shareArticle?url=https://danyuan30.gitee.io/2021/10/11/JavaScript/oop/&title=面向对象"><i class="fab fa-linkedin fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://pinterest.com/pin/create/bookmarklet/?url=https://danyuan30.gitee.io/2021/10/11/JavaScript/oop/&is_video=false&description=面向对象"><i class="fab fa-pinterest fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=面向对象&body=Check out this article: https://danyuan30.gitee.io/2021/10/11/JavaScript/oop/"><i class="fas fa-envelope fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://getpocket.com/save?url=https://danyuan30.gitee.io/2021/10/11/JavaScript/oop/&title=面向对象"><i class="fab fa-get-pocket fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://reddit.com/submit?url=https://danyuan30.gitee.io/2021/10/11/JavaScript/oop/&title=面向对象"><i class="fab fa-reddit fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.stumbleupon.com/submit?url=https://danyuan30.gitee.io/2021/10/11/JavaScript/oop/&title=面向对象"><i class="fab fa-stumbleupon fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://digg.com/submit?url=https://danyuan30.gitee.io/2021/10/11/JavaScript/oop/&title=面向对象"><i class="fab fa-digg fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.tumblr.com/share/link?url=https://danyuan30.gitee.io/2021/10/11/JavaScript/oop/&name=面向对象&description="><i class="fab fa-tumblr fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://news.ycombinator.com/submitlink?u=https://danyuan30.gitee.io/2021/10/11/JavaScript/oop/&t=面向对象"><i class="fab fa-hacker-news fa-lg" aria-hidden="true"></i></a></li>
</ul>

    </div>

    <div id="actions-footer">
        <a id="menu" class="icon" href="#" onclick="$('#nav-footer').toggle();return false;"><i class="fas fa-bars fa-lg" aria-hidden="true"></i> 菜单</a>
        <a id="toc" class="icon" href="#" onclick="$('#toc-footer').toggle();return false;"><i class="fas fa-list fa-lg" aria-hidden="true"></i> 目录</a>
        <a id="share" class="icon" href="#" onclick="$('#share-footer').toggle();return false;"><i class="fas fa-share-alt fa-lg" aria-hidden="true"></i> 分享</a>
        <a id="top" style="display:none" class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up fa-lg" aria-hidden="true"></i> 返回顶部</a>
    </div>

  </div>
</div>

        
        <footer id="footer">
  <div class="footer-left">
    Copyright &copy;
    
    
    2021-2022
    柯里的语法糖
  </div>
  <div class="footer-right">
    <nav>
      <ul>
        <!--
       --><li><a href="/">首页</a></li><!--
     --><!--
       --><li><a href="/tags/">标签</a></li><!--
     --><!--
       --><li><a href="/categories/">分类</a></li><!--
     --><!--
       --><li><a href="/archives/">归档</a></li><!--
     --><!--
       --><li><a target="_blank" rel="noopener" href="http://github.com/twitzz">项目</a></li><!--
     --><!--
       --><li><a href="/about/">关于</a></li><!--
     --><!--
       --><li><a href="/search/">搜索</a></li><!--
     -->
      </ul>
    </nav>
  </div>
</footer>

    </div>
    <!-- styles -->



  <link rel="preload" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css" crossorigin="anonymous" onload="this.onload=null;this.rel='stylesheet'"/>


    <!-- jquery -->
 
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" crossorigin="anonymous"></script> 




<!-- clipboard -->

  
    <script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.7/clipboard.min.js" crossorigin="anonymous"></script> 
  
  <script type="text/javascript">
  $(function() {
    // copy-btn HTML
    var btn = "<span class=\"btn-copy tooltipped tooltipped-sw\" aria-label=\"复制到粘贴板!\">";
    btn += '<i class="far fa-clone"></i>';
    btn += '</span>'; 
    // mount it!
    $(".highlight table").before(btn);
    var clip = new ClipboardJS('.btn-copy', {
      text: function(trigger) {
        return Array.from(trigger.nextElementSibling.querySelectorAll('.code')).reduce((str,it)=>str+it.innerText+'\n','')
      }
    });
    clip.on('success', function(e) {
      e.trigger.setAttribute('aria-label', "复制成功!");
      e.clearSelection();
    })
  })
  </script>


<script src="/js/main.js"></script>

<!-- search -->

<!-- Google Analytics -->

<!-- Baidu Analytics -->

<!-- Cloudflare Analytics -->

<!-- Umami Analytics -->

<!-- Disqus Comments -->

<!-- utterances Comments -->

</body>
</html>
